<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>http-https | H5教案</title>

  
  <meta name="author" content="John Doe">
  

  
  <meta name="description" content="3、http和https3.1、get由于大多数请求都是没有主体的 GET 请求,这里就写了一个简易的get请求主要用于请求远程服务的文件.例如[http://localhost:4001/server/config.json]
12345678910111213141516171819202122">
  

  
  
  <meta name="keywords" content="">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="http-https"/>

  <meta property="og:site_name" content="H5教案"/>

  
  <meta property="og:image" content="/ericxie-book/favicon.ico"/>
  

  <link href="/ericxie-book/favicon.ico" rel="icon">
  <link rel="alternate" href="/ericxie-book/atom.xml" title="H5教案" type="application/atom+xml">
  <link rel="stylesheet" href="/ericxie-book/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/ericxie-book/">H5教案</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>http-https</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/25/http-https/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-25T12:00:31.000Z">
          2021-01-25
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="3、http和https"><a href="#3、http和https" class="headerlink" title="3、http和https"></a>3、http和https</h2><h3 id="3-1、get"><a href="#3-1、get" class="headerlink" title="3.1、get"></a>3.1、get</h3><p>由于大多数请求都是没有主体的 GET 请求,这里就写了一个简易的get请求<br>主要用于请求远程服务的文件.例如[<a target="_blank" rel="noopener" href="http://localhost:4001/server/config.json]">http://localhost:4001/server/config.json]</a></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">var http = require(<span class="string">&#x27;http&#x27;</span>)</span><br><span class="line">var https = require(<span class="string">&#x27;https&#x27;</span>)</span><br><span class="line"></span><br><span class="line">const server = http.createServer((request, response) =&gt; &#123;</span><br><span class="line">  var url = request.url.substr(1)</span><br><span class="line">  var data = <span class="string">&#x27;&#x27;</span></span><br><span class="line">  response.writeHeader(200, &#123;</span><br><span class="line">    <span class="string">&#x27;content-type&#x27;</span>: <span class="string">&#x27;application/json;charset=utf-8&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>: <span class="string">&#x27;*&#x27;</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">#   response.setEncoding(&quot;utf8&quot;);//或者这样设置utf-8</span></span><br><span class="line">  https.get(`https://m.lagou.com/listmore.json<span class="variable">$&#123;url&#125;</span>`, (res) =&gt; &#123;</span><br><span class="line"></span><br><span class="line">    res.on(<span class="string">&#x27;data&#x27;</span>, (chunk) =&gt; &#123;</span><br><span class="line">      data += chunk</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    res.on(<span class="string">&#x27;end&#x27;</span>, () =&gt; &#123;</span><br><span class="line">      response.end(JSON.stringify(&#123;</span><br><span class="line">        ret: <span class="literal">true</span>,</span><br><span class="line">        data</span><br><span class="line">      &#125;))</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">server.listen(8080, () =&gt; &#123;</span><br><span class="line">  console.log(<span class="string">&#x27;localhost:8080&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="3-2-post：服务器提交"><a href="#3-2-post：服务器提交" class="headerlink" title="3.2 post：服务器提交"></a>3.2 post：服务器提交</h3><p>通过request不断请求服务器进行攻击</p>
<h4 id="a-js"><a href="#a-js" class="headerlink" title="a.js"></a>a.js</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">const http = require(<span class="string">&#x27;http&#x27;</span>)</span><br><span class="line">const querystring = require(<span class="string">&#x27;querystring&#x27;</span>)</span><br><span class="line"></span><br><span class="line">const postData = querystring.stringify(&#123;</span><br><span class="line">    province: <span class="string">&#x27;上海&#x27;</span>,</span><br><span class="line">    city: <span class="string">&#x27;上海&#x27;</span>,</span><br><span class="line">    district: <span class="string">&#x27;宝山区&#x27;</span>,</span><br><span class="line">    address: <span class="string">&#x27;同济支路199号智慧七立方3号楼2-4层&#x27;</span>,</span><br><span class="line">    latitude: 43.0,</span><br><span class="line">    longitude: 160.0,</span><br><span class="line">    message: <span class="string">&#x27;求购一条小鱼&#x27;</span>,</span><br><span class="line">    contact: <span class="string">&#x27;13666666&#x27;</span>,</span><br><span class="line">    <span class="built_in">type</span>: <span class="string">&#x27;sell&#x27;</span>,</span><br><span class="line">    time: 1571217561</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">const options = &#123;</span><br><span class="line">    protocol: <span class="string">&#x27;http:&#x27;</span>,</span><br><span class="line">    hostname: <span class="string">&#x27;localhost&#x27;</span>,</span><br><span class="line">    method: <span class="string">&#x27;POST&#x27;</span>,</span><br><span class="line">    port: 4010,</span><br><span class="line">    path: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    headers: &#123;</span><br><span class="line">        <span class="string">&#x27;Content-Type&#x27;</span>: <span class="string">&#x27;application/x-www-form-urlencoded&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;Content-Length&#x27;</span>: Buffer.byteLength(postData)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">http.createServer((req, res) =&gt; &#123;</span><br><span class="line">    res.writeHead(200, &#123;</span><br><span class="line">        <span class="string">&quot;Access-Control-Allow-Origin&quot;</span>: <span class="string">&quot;*&quot;</span></span><br><span class="line">    &#125;)</span><br><span class="line">    var request = http.request(options,result=&gt;requsetHandler(result,res));</span><br><span class="line">    request.write(postData);</span><br><span class="line">    request.end();</span><br><span class="line">&#125;).listen(4011);</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> requsetHandler(result,res) &#123;</span><br><span class="line">    var data = <span class="string">&quot;&quot;</span>;</span><br><span class="line">    result.on(<span class="string">&quot;data&quot;</span>, _data =&gt; &#123;</span><br><span class="line">        data += _data;</span><br><span class="line">    &#125;);</span><br><span class="line">    result.on(<span class="string">&quot;end&quot;</span>, () =&gt; &#123;</span><br><span class="line">        res.end(data);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="b-js"><a href="#b-js" class="headerlink" title="b.js"></a>b.js</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">var http=require(<span class="string">&quot;http&quot;</span>);</span><br><span class="line">http.createServer((req,res)=&gt;&#123;</span><br><span class="line">    var data=<span class="string">&quot;&quot;</span>;</span><br><span class="line">    req.on(<span class="string">&quot;data&quot;</span>,_data=&gt;&#123;</span><br><span class="line">        console.log(_data+<span class="string">&quot;&quot;</span>);</span><br><span class="line">        data+=_data;</span><br><span class="line">    &#125;)</span><br><span class="line">    req.on(<span class="string">&quot;end&quot;</span>,()=&gt;&#123;</span><br><span class="line">        console.log(data);</span><br><span class="line">        res.end(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;).listen(4010);</span><br></pre></td></tr></table></figure>
<h3 id="3-3-跨域：jsonp"><a href="#3-3-跨域：jsonp" class="headerlink" title="3.3 跨域：jsonp"></a>3.3 跨域：jsonp</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">const http = require(<span class="string">&#x27;http&#x27;</span>)</span><br><span class="line">const url = require(<span class="string">&#x27;url&#x27;</span>)</span><br><span class="line"></span><br><span class="line">const app = http.createServer((req, res) =&gt; &#123;</span><br><span class="line">  <span class="built_in">let</span> urlObj = url.parse(req.url, <span class="literal">true</span>)</span><br><span class="line"></span><br><span class="line">  switch (urlObj.pathname) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;/api/user&#x27;</span>:</span><br><span class="line">      res.end(`<span class="variable">$&#123;urlObj.query.cb&#125;</span>(&#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;gp145&quot;</span>&#125;)`)</span><br><span class="line">      <span class="built_in">break</span></span><br><span class="line">    default:</span><br><span class="line">      res.end(<span class="string">&#x27;404.html&#x27;</span>)</span><br><span class="line">      <span class="built_in">break</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.listen(8080)</span><br></pre></td></tr></table></figure>
<h3 id="3-4-跨域：CORS"><a href="#3-4-跨域：CORS" class="headerlink" title="3.4 跨域：CORS"></a>3.4 跨域：CORS</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">const http = require(<span class="string">&#x27;http&#x27;</span>)</span><br><span class="line">const url = require(<span class="string">&#x27;url&#x27;</span>)</span><br><span class="line">const querystring = require(<span class="string">&#x27;querystring&#x27;</span>)</span><br><span class="line"></span><br><span class="line">const app = http.createServer((req, res) =&gt; &#123;</span><br><span class="line">  <span class="built_in">let</span> data = <span class="string">&#x27;&#x27;</span></span><br><span class="line">  <span class="built_in">let</span> urlObj = url.parse(req.url, <span class="literal">true</span>)</span><br><span class="line"></span><br><span class="line">  res.writeHead(200, &#123;</span><br><span class="line">    <span class="string">&#x27;content-type&#x27;</span>: <span class="string">&#x27;application/json;charset=utf-8&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>: <span class="string">&#x27;*&#x27;</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  req.on(<span class="string">&#x27;data&#x27;</span>, (chunk) =&gt; &#123;</span><br><span class="line">    data += chunk</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  req.on(<span class="string">&#x27;end&#x27;</span>, () =&gt; &#123;</span><br><span class="line">    responseResult(querystring.parse(data))</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">function</span> responseResult(data) &#123;</span><br><span class="line">    switch (urlObj.pathname) &#123;</span><br><span class="line">      <span class="keyword">case</span> <span class="string">&#x27;/api/login&#x27;</span>:</span><br><span class="line">        res.end(JSON.stringify(&#123;</span><br><span class="line">          message: data</span><br><span class="line">        &#125;))</span><br><span class="line">        <span class="built_in">break</span></span><br><span class="line">      default:</span><br><span class="line">        res.end(<span class="string">&#x27;404.&#x27;</span>)</span><br><span class="line">        <span class="built_in">break</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.listen(8080, () =&gt; &#123;</span><br><span class="line">  console.log(<span class="string">&#x27;localhost:8080&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="3-5-跨域：middleware（http-proxy-middware）"><a href="#3-5-跨域：middleware（http-proxy-middware）" class="headerlink" title="3.5 跨域：middleware（http-proxy-middware）"></a>3.5 跨域：middleware（http-proxy-middware）</h3><p>中间层代理模块，通过代理服务跨域访问网站</p>
<p>下载middleware</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i http-proxy-middleware -D</span><br></pre></td></tr></table></figure>
<p>跨域方式</p>
<h4 id="c-js"><a href="#c-js" class="headerlink" title="c.js"></a>c.js</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">var http=require(<span class="string">&quot;http&quot;</span>);</span><br><span class="line"></span><br><span class="line">var &#123;createProxyMiddleware&#125;=require(<span class="string">&quot;http-proxy-middleware&quot;</span>);</span><br><span class="line"></span><br><span class="line">http.createServer((req,res)=&gt;&#123;</span><br><span class="line">    res.writeHead(200, &#123;</span><br><span class="line">        <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>: <span class="string">&#x27;*&#x27;</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">if</span>(/^\/api/.<span class="built_in">test</span>(req.url))&#123;</span><br><span class="line">        var apiProxy=createProxyMiddleware(<span class="string">&quot;&quot;</span>,&#123;</span><br><span class="line">            target:<span class="string">&quot;http://localhost:4010&quot;</span>,</span><br><span class="line">            changeOrigin:<span class="literal">true</span>,//修改托管</span><br><span class="line">            pathRewrite:&#123;</span><br><span class="line">                <span class="string">&#x27;^/api&#x27;</span>:<span class="string">&quot;&quot;</span>//是否修改请求路径</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        apiProxy(req,res);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;).listen(4011);</span><br></pre></td></tr></table></figure>
<h4 id="d-js"><a href="#d-js" class="headerlink" title="d.js"></a>d.js</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">var http=require(<span class="string">&quot;http&quot;</span>);</span><br><span class="line">http.createServer((req,res)=&gt;&#123;</span><br><span class="line">    var data=<span class="string">&quot;&quot;</span>;</span><br><span class="line">    req.on(<span class="string">&quot;data&quot;</span>,_data=&gt;&#123;</span><br><span class="line">        console.log(_data+<span class="string">&quot;&quot;</span>);</span><br><span class="line">        data+=_data;</span><br><span class="line">    &#125;);</span><br><span class="line">    req.on(<span class="string">&quot;end&quot;</span>,()=&gt;&#123;</span><br><span class="line">        console.log(data);</span><br><span class="line">        res.end(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;).listen(4010);</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>

  






    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2021 John Doe
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>